-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use exists()
to check for properties
#17798
Use exists()
to check for properties
#17798
Conversation
exists()
to check for existing properties.exists()
to check for properties
Calling all codobots 🤖 |
samples/client/petstore/typescript-fetch/builds/allOf-nullable/models/Club.ts
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache
Show resolved
Hide resolved
* Use `exists()` to check for existing properties. * Generate Petstore code. * Fix use of `null` instead of `undefined`. * Enhance function code. * Regenerate Petstore code. * Use `exists()` in generated API clients. * Use `exists()` in generated API clients. * Refer to properties differently.
It seems to me that maybe something in this PR has introduced an unexpected behavior? I have an OpenAPI schema definition as such: {
"components": {
"schemas": {
"MyObject": {
"properties": {
"backtoken": {
"title": "Backtoken",
"maxLength": 500,
"type": "string",
"nullable": true
},
"skiptoken": {
"title": "Skiptoken",
"maxLength": 500,
"type": "string",
"nullable": true
}
},
"type": "object"
}
}
}
} This is being generated with the export interface MyObject {
backtoken?: string;
skiptoken?: string;
} Even though the openapi spec has an explicit mention of Shouldn't it still honor that |
This was mentioned and clarified before in this comment: #17798 (comment) |
This change breaks the OpenAPI spec. There is a difference between a nullable and optional property. With the release of 7.4.0 all properties that can be null (a value) are just made optional (omitted from request/response). |
@noordawod still getting back on this, reading carefully through the comments and reviews. Your sentencte
regarding this line
I guess you're seeing the "pass in" from an API perspective, right? null, undefined or a string coming in from outside and is beeing parsed. Seeing it the other way around, wanting to be able to make the resulting json in a request differentiate between null, undefined and a string is not possible with this solution, since typescript won't let you write Other generators like sping, support differentiating between undefined, null and a string value. How would I achieve this by using the typescript-fetch-operator after this fix? |
It appears that this PR has broken nullable types. Take the instance where we do a Here's a definition of what a patch could look like, you can optionally pass any of the parameters. In this case, the export interface ArticlePatch {
title?: string;
subTitle?: string | null;
} Scenario 1: Patching
Scenario 2: Patching
{
"subTitle": {
"type": "string",
"nullable": true
}
} In previous versions export interface ArticlePatch {
title?: string;
subTitle?: string | null;
} Since this PR has been merged: export interface ArticlePatch {
title?: string;
subTitle?: string; // No longer nullable
} EDIT:I have raised a PR to address this and it has been merged: |
Use
exists()
function to test for property existence on dictionaries/Maps rather than sprinkling the code withjson.key === null || json.key === undefined
.In addition, the code removes the implicit
null
type of optional and required properties because in the case of TS/JS, restricting the property to onlynull
means that it won't be able to receive theundefined
value too, which in the case of JSON, ultimately resolves tonull
in transport.The last line in the example covers the situation when you may pass in a value that is either
null
,undefined
, or indeed astring
. If you restrict it only tostring | null
, thenundefined
won't be a plausible value, which it should.PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)